<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/

//$_COOKIE['huynh'] = "HUYNH"; 
//$input = "3-10-2008";



//$date =date("Y",strtotime($input));
//echo $date; 
//echo"=";
//echo date("dmY",strtotime($input));

//echo strtotime($input);

//echo time(); echo "=";
//echo mktime();

	$pathdb = "uploads/"; // Duong dan ghi vao database		
	$imagepath="../uploads/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif,pdf,jpeg"; // Cac phan mo rong duoc cho phep
	
	$id=$_GET['id'];
	
	
	

if (isset($_POST['butSave'])) {
	$published = $_POST['published'];
	$hit = $_POST['hit'];
	$date_add = $_POST['date_add'];
	
	$ngay = $_POST['ngay'];
	$thang = $_POST['thang'];
	$nam = $_POST['nam'];
	$input= "{$ngay}-{$thang}-{$nam}";
	$date_add=strtotime($input);
	
	$catid = $_POST['catid'];
	$title = trim($_POST['title']);
	$vbb_id = $_POST['vbb_id'];
	
	$numcv1 = $_POST['numcv1'];
	
	$numcv2 = $_POST['numcv2'];
	
	$user_id= $_POST['user_id'];
	
	
//	echo $user_id;
//	exit;
	
	$user_id=$savetodb_user_id = serialize($user_id);

//echo $savetodb;
	//if ($user_id){
	 //foreach ($user_id as $t){echo 'You selected ',$t,'<br />';}
	//}
	
	//exit;
	
	
	$cat = getCat($catid,'news_categories'); 
	//$catP = getC($cat->id);
	
	$catp = mysql_fetch_object(mysql_query("SELECT * FROM news_categories WHERE id=".$cat->parent_id));
	//$catp->alias;
	if($catp->alias!='')
	$catlink = $catp->alias."/".$cat->alias;
	else
	$catlink = $cat->alias;
	
	if($_POST['alias']!='')
	$alias = trim($_POST['alias']);
	else
	$alias = vietdecode(trim($_POST['title']));
	
	$intro = trim($_POST['intro']);
	$body = trim(($_POST['body']));
	
	$err="";	
	if ($title=="") $err .="Xin nhập tiêu đề bằng<br>";	
	if ($intro =="") $err .="Xin nhập intro bằng<br>";	
	

	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
		
		
			$oldid = $_POST['id'];
			$sql = "update news set  published='".$published."',hit='".$hit."',title='".$title."',alias='".$alias."',intro='".$intro."',";
			$sql.= "catlink='".$catlink."', date_add='".$date_add."',catid='".$catid."', numcv1='".$numcv1."', numcv2='".$numcv2."', user_id='".$savetodb_user_id."'	 where id='".$oldid."'";
			 
		//	 echo "XXXXXXXXXX";
		}else {
		
		
		
			// Insert data
			$sql = "insert into news (title,alias,catlink,intro,body,published,hit,date_add,catid,numcv1,numcv2,user_id)";
			$sql.= "values ('".$title."','".$alias."','".$catlink."','".$intro."','".$body."','".$published."','".$hit."','".$date_add."','".$catid."','".$numcv1."','".$numcv2."','".$savetodb_user_id."')";
		}
		
	//	echo $sql;
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
			/*if(!empty($_POST['id'])){
			// upldate
				foreach ($_POST['title'] as $langid => $title){		
						$intro = trim($_POST['intro'][$langid]);
						$body = trim($_POST['body'][$langid]);		
						$sql = "update news_des set title='".$title."',intro='".$intro."',body='".$body."'";
						$sql .= "where id='".$oldid."' and  languages_id='".$langid."'";
						mysql_query($sql);
					}	
			} else {
			// Insert			
				foreach ($_POST['title'] as $langid => $title){						
					$sql = "insert into news_des (id,languages_id,title,intro,body)";
					$sql .= "values('".$oldid."','".$langid."','".$title."','".$intro."','".$body."')";
					mysql_query($sql);
				}	
			}*/
			
		//Upload image here
		/*
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			
			$tenfile=$numcv1."-".str_replace("/","-",$numcv2);
			$filename = generate_name('',$tenfile,$_FILES['img']['name']);
			mkdir($imagepath.$nam, 0777);
			mkdir($imagepath.$nam."/".$thang, 0777);
			
			
			//move_uploaded_file($_FILES['img']['tmp_name'],$imagepath.$nam.'/' .$thang.'/'. $filename);
						water($_FILES['img']['tmp_name'],$imagepath.$nam.'/' .$thang.'/'. $filename);
			//resize_image($imagepath.'large/'.$filename,$imagepath.'small/'.$filename,50,40,100);
			//resize_image($imagepath.'large/'.$filename,$imagepath.'large/'.$filename,120,120,100);

			$_POST["img"] = $filename;
			$tenfile_ok = $nam."/".$thang."/".$filename;
			
			//echo "<HR>@@@@@@@@		$imagepath	{$_POST["img"]} ";
			//exit;
					
			$sqlUpdateField = " image ='".$tenfile_ok."'";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update news set $sqlUpdateField where id='".$oldid."'";
					$db->query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		
		*/
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t";
		}
		
	} // if ($err=='')
	
	if ($err==''){
	$res = $db->query("SELECT * FROM news_categories WHERE parent_id=50");
	while($r=$db->fetchNextObject($res)){
	$strCat .='catid='.$r->id.' or ';
	}
	//$strCat=$strCat.' catid=50';
	//$strSQL = "SELECT * from news WHERE $strCat order by date_add DESC limit 7";
	///echo homelist($strSQL,'../modules/html/news.htm');
	
	$strCat='';
	
	$res = $db->query("SELECT * FROM news_categories WHERE parent_id=36");
	while($r=$db->fetchNextObject($res)){
	$strCat .='catid='.$r->id.' or ';
	}
	$strCat=$strCat.' catid=36';
	$strSQL = "SELECT * from news WHERE $strCat order by date_add DESC limit 7";
	//echo homelist($strSQL,'../modules/html/tutorial.htm');
	
	 echo '<script>window.location="index.php?act=news&catid='.$_REQUEST['catid'].'&code=1"</script>';
	 } 	else {
	  echo "<p align=center class='red'>".$err."</p>";
	  }

} 
		
 ?>
 
 <script type="text/javascript" src="ajax.js"></script> 


<? action('CÔNG VĂN'); // Tieu de trang


	if (isset($_GET['id'])){
	$result=mysql_query("SELECT * FROM news WHERE id=$id");
	$row=mysql_fetch_object($result);
	$image=$row->image;
	$imagelarge=$row->imagelarge;
	$title=$row->title;
	$alias=$row->alias;
	$intro=$row->intro;
	$body=$row->body;
	$published=$row->published;
	$hit=$row->hit;
	$catid=$row->catid;
		$numcv1=$row->numcv1;
			$numcv2=$row->numcv2;
			$user_id=$row->user_id;
			
			//echo $user_id;
	//####### XY LY NGAY THANG
		$date_add=$row->date_add;
		
		
		$nam=date("Y",$date_add);
		$thang=date("m",$date_add);
				$ngay=date("d",$date_add);
		
		//##########
	
	$parent=$catid;
	$vbb_id=$row->vbb_id;
	
	


$id=$_GET['id'];


	
	}
	else 
	{
	 if ($nam==0) $nam=date(Y);
	 if ($thang==0) $thang=date(m);
		 
		
		$sqlx=mysql_query("SELECT max(id) as max FROM news limit 1");
		$rrr=mysql_fetch_array($sqlx);
		$id=$rrr['max']+1;
		
		$sqlx = "DELETE FROM `linkimg` WHERE `news_id` = $id ";
		mysql_query($sqlx);
		
		
		 
		 
		 }
	 $parent=$catid;
	 

	 
	
	 

	?>
	
		<script src="/check.js" language="javascript"></script>
	<SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
		 {
		 alert("Số công văn là một chuổi số, không phải chử hoặc ký thiệu .");
            return false;
}
         return true;
      }
	  
	  
	   function isNumberKey_ngay(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
		 {
		 alert("Nhập sai định dạng ngày, nó là số không phải chử .");
		 
            return false;
}
         return true;
      }
      //-->
   </SCRIPT>
   
   
   
<style type="text/css">
<!--
.style1 {
	color: #FF0000;
	font-weight: bold;
}
.style2 {
	color: #FF0000;
	font-style: italic;
}
-->
</style>
	
  <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="commentform" id="commentform" onSubmit="return check();">


<input type="hidden" name="act" value="news_m">
<input type="hidden" name="id" value="<? echo $_GET['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">
    

    <tr>
      <td width="119" align="right" valign="top"><div align="left">Số Công Văn</div></td>
      <td width="499" valign="top" >
	   <input name="numcv1" type="text" id="numcv1" style="width:15%" value="<?=$numcv1;?>" size="5"  onkeypress="return isNumberKey(event)" /> 
	   <span class="style1">/</span>
	   <input name="numcv2" type="text" style="width:35%" value="<?=$numcv2;?>" size="25" />      </td>
    </tr>
	<tr>
	  <td align="right"><div align="left">Ban Hành ngày </div></td>
	  <td ><input name="ngay" type="text" id="hit" value="<?=$ngay;?>"  style="width:35px" onkeypress="return isNumberKey_ngay(event)" /> 
	  Tháng
	  <input name="thang" type="text" id="hit" value="<?=$thang?>" style="width:35px"/> 
	  Năm
	  <input name="nam" type="text" id="hit" value="<?=$nam;?>" style="width:55px; "/>	  </td>
    </tr>
	<tr>
	  <td align="right"><div align="left">Nguồn công văn </div></td>
	  <td ><select size="1" name="catid">
		<?
		echo "<option value='0'>[Danh m&#7909;c g&#7889;c]</option>";
		$cats=getListCat();
		foreach ($cats as $cat)
		{
			if ($cat[0]==$parent)
				echo "<option value=".$cat[0]." selected>".$cat[1]."</option>";
			else
				echo "<option value=".$cat[0].">".$cat[1]."</option>";
		}
		?>		
		</select></td>
    </tr>
	<tr>
      <td align="right" valign="top"><div align="left">Tiêu đề CV </div></td>
      <td>
	  <div style="margin:3px 0">
	    <input name="title" type="text" style="width:95%" value="<?=$title;?>" size="50">
	  </div>	   </td>
    </tr>
   
    <tr>
      <td align="right" valign="top"><div align="left">Trích yếu nội dung </div></td>
      <td valign="top" >
	  <textarea name="intro" rows="4" id="intro" style="width:95%"><?=$intro;?></textarea>	</td>
    </tr>
	<!--
    <tr>
      <td align="right" valign="top"><div align="left">Body text </div></td>
      <td >

	  <textarea name="body" id="body" style="width:95%" ><?=$body;?></textarea><br />	  </td>
    </tr>-->
	 
	<tr>
	  <td align="right"><div align="left">Phụ trách công văn</div></td>	  
	  <td >
	  
	    <select size="8" name="user_id[]" multiple="multiple">
	  <?
	  
	
	
	$array = unserialize($user_id);


	 if ($user_id<>"")  				echo "<option value='0' >[Tất cả nhân viên]</option>";

	else	echo "<option value='0' selected>[--Tất cả nhân viên--]</option>";
	
		//$result = $db->query($sql);
		$sql=mysql_query("SELECT * FROM user ORDER BY ordering ASC");
		while($r=mysql_fetch_array($sql))
		{
			
			// if ($user_id=="")  echo "<option value=".$r['id'].">&nbsp;".$r['fullname']."</option>";
			 
		//	 else
			// { 
			echo "<option value=\"{$r['id']}\"  ";
				foreach ($array as $t)
				{
		
						if ($r['id']==$t)
						{
						echo " selected ";
						break 1;
						}
				
				}
				
			echo"		>&nbsp;".$r['fullname']."</option>";
				
				
				
				
			//	}
		
			}
			
		
		
		
	?>
	
		
		</select>
		</td>
    </tr>
	
	
	<tr>
      <td align="right"><div align="left">Đính kèm </div></td>
      <td >
	
	  <div id="inputfile" style="height:20px;width:350px;">
    	        <div style="float: left;" id="flash_upload"></div>   <script>upload_watermark(<? echo $id; ?>);</script>
	</div>

 

<div style="clear:both"></div>
<div id="result"></div>
<div id="loading"></div>
<div id="getcode" style="display:none">
 </div>
 
	<div align="right"><BR />
	  <span class="style2"><U>∩Lưu ý:</u></span> &nbsp;Giữ Ctrl Click chuột chọn nhiều file cùng lúc. Đặt tên file theo thứ tự <em>alphabet.</em><BR /> 
	  Nếu chọn nhầm file , phải click "DELETE IMG" rồi sau đó mới chọn lại file.</div></td>
    </tr>
	
	<tr>
	  <td align="right"><div align="left">Xuất bản</div></td>	  
	  <td ><input name="published" type="checkbox" id="published" value="1" checked="<? if ($published>0) echo 'checked' ?>" /></td>
    </tr>
	
	<!---
	<tr>
	  <td align="right"><div align="left">Hit</div></td>
	  <td ><input name="hit" type="text" id="hit" value="<?=$hit;?>" /></td>
    </tr>-->
    <tr>
      <td><div align="left"></div></td>
      <td><label>

        <input name="butSave" type="submit" id="butSave" value="   Cập nhật  " class="button"  />
        <input name="date_add" type="hidden" id="date_add" value="<?=mktime();?>" />
      </label></td>
    </tr></form>
</table>

